package org.xx.armory.obs;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.Authentication;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import org.xx.armory.commons.ForLogging;
import org.xx.armory.spring5.ArmoryWebApplication;

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Config
        extends ArmoryWebApplication {
    @ForLogging
    private final Logger logger = LoggerFactory.getLogger(Config.class);

    @Bean
    public MockMvc mockMvc(
            WebApplicationContext context
    ) {
        return webAppContextSetup(context).build();
    }

    @Bean
    public ObsAuthorization obsAuthorization() {
        return new ObsAuthorization() {
            @Override
            public boolean allowUpload(
                    String root,
                    long objId,
                    int objType,
                    String fileName,
                    long fileSize,
                    Authentication token
            ) {
                return "o".equals(root);
            }

            @Override
            public boolean allowList(
                    String root,
                    long objId,
                    int objType,
                    Authentication token
            ) {
                return objId == 101;
            }

            @Override
            public boolean allowDelete(
                    String root,
                    long objId,
                    int objType,
                    String fileName,
                    Authentication token
            ) {
                return fileName.endsWith(".txt");
            }

            @Override
            public boolean allowDownload(
                    String root,
                    long objId,
                    int objType,
                    String fileName,
                    Authentication token
            ) {
                return "o".equals(root);
            }
        };
    }
}
